/**
 * 时间格式带T的(MM-dd HH:mm)
 * @param {*} dateStr 
 * @returns 
 */
function formatDateTime(dateStr) {
  if (!dateStr) return '';
  const d = new Date(dateStr);
  if (isNaN(d.getTime())) return '';
  const y = d.getFullYear();
  const m = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');
  const h = d.getHours().toString().padStart(2, '0');
  const min = d.getMinutes().toString().padStart(2, '0');
  return `${m}-${day} ${h}:${min}`;
}

/**
 * 时间格式带T的，不带秒(yyyy-MM-dd HH:mm)
 * @param {*} dateStr 
 * @returns 
 */
function formatDateTimeNoSeconds(dateStr) {
  if (!dateStr) return '';
  const d = new Date(dateStr);
  if (isNaN(d.getTime())) return '';
  const y = d.getFullYear();
  const m = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');
  const h = d.getHours().toString().padStart(2, '0');
  const min = d.getMinutes().toString().padStart(2, '0');
  return `${y}-${m}-${day} ${h}:${min}`;
}

/**
 * 获取当前时间加5秒的时间字符串
 * @returns {string}
 */
function getNowPlus5s() {
  const now = new Date();
  now.setSeconds(now.getSeconds() + 5);
  return `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')} ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`;
}

/**
 * 时间格式(yyyy-MM-dd)
 * @param {*} dateStr 
 * @returns 
 */
function formatyyyyMMdd(dateStr) {
  if (!dateStr) return '';
  const d = new Date(dateStr);
  if (isNaN(d.getTime())) return '';
  const y = d.getFullYear();
  const m = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');
  return `${y}-${m}-${day}`;
}


/**
 * 时间格式(HH:mm)
 * @param {*} dateStr 
 * @returns 
 */
function formatHHmm(dateStr) {
  if (!dateStr) return '';
  const d = new Date(dateStr);
  if (isNaN(d.getTime())) return '';
  const h = d.getHours().toString().padStart(2, '0');
  const min = d.getMinutes().toString().padStart(2, '0');
  return `${h}:${min}`;
}


module.exports = {
  formatDateTime,
  getNowPlus5s,
  formatDateTimeNoSeconds,
  formatyyyyMMdd,
  formatHHmm
}; 